package com.cloud.drore.eboss.operation.service.impl;

import com.cloud.drore.eboos.common.base.BaseServiceImpl;
import com.cloud.drore.eboos.common.base.MyMapper;
import com.cloud.drore.eboos.common.base.PagePo;
import com.cloud.drore.eboos.common.base.ResultMessage;


import com.cloud.drore.eboos.common.entity.operation.HashMapVo;
import com.cloud.drore.eboos.common.util.ListToHashMapUtil;
import com.cloud.drore.eboss.operation.entity.DistributorAccount;


import com.cloud.drore.eboss.operation.entity.vo.DistributorAccountInfoVo;
import com.cloud.drore.eboss.operation.entity.vo.DistributorAccountVo;
import com.cloud.drore.eboss.operation.mapper.DistributorMapper;
import com.cloud.drore.eboss.operation.mapper.DistributorRechargeMapper;
import com.cloud.drore.eboss.operation.mapper.FinaceSettlementHistoryMapper;
import com.cloud.drore.eboss.operation.mapper.MerchantMapper;
import com.cloud.drore.eboss.operation.service.IBaseDataFeign;
import com.cloud.drore.eboss.operation.service.IDistributorAccountService;
import com.cloud.drore.eboss.util.DictionaryMapUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 浙江卓锐科技股份有限公司 版权所有 © Copyright 2018<br>
 *
 * @Description: <br>
 * @Project: eboos <br>
 * @CreateDate: Created in 2018/1/4 12:45 <br>
 * @Author: <a href="836327318@qq.com">吴昌益</a>
 */
@Service
public class DistributorAccountServiceImpl extends BaseServiceImpl implements IDistributorAccountService {

    @Resource
    DistributorMapper distributorMapper;
    @Resource
    FinaceSettlementHistoryMapper finaceSettlementHistoryMapper;
    @Autowired
    MerchantMapper merchantMapper;
    @Resource
    DistributorRechargeMapper distributorRechargeMapper;
    @Autowired
    IBaseDataFeign iBaseDataFeign;

    @Override
    public MyMapper getMapper() {
        return distributorMapper;
    }


    /**
     * @param
     * @ClassName: DistributorAccountServiceImpl
     * @Return: java.util.List<com.cloud.drore.eboss.operation.entity.DistributorAccount>
     * @Decription: 查询分销商账户列表
     * @CreateDate: Created in 2018/1/20 0020 上午 10:48
     * @Author: <a href="410508961@qq.com">夏凯</a>
     * @Modify:
     */
    public ResultMessage<List<DistributorAccountInfoVo>> findByName(PagePo page) {
        //如果分销商id不为空，则查询账户对象
        DistributorAccount distributorAccount = (DistributorAccount) page.getFields();
        String distributorName = distributorAccount.getDistributorName();
        //商户性质翻译
        Map<String, String> merchantIndustryMap = DictionaryMapUtil.getMapByParentCode("BUSINESS_NATURE");
        //分销商充值信息总和
        List<HashMapVo> merchantRechargeTotalList = distributorRechargeMapper.queryMerchantRechargeTotalMap();
        Map<String, String> merchantRechargeTotalMap = ListToHashMapUtil.listToHashMap(merchantRechargeTotalList);
        PageHelper.startPage(page);
        if (!(null == distributorName || "".equals(distributorName))) {
            List<DistributorAccountInfoVo> distributorAccounts = distributorMapper.selectDistributorAccountByDistributorName(distributorAccount.getDistributorName());
            long total = new PageInfo(distributorAccounts).getTotal();
            //字典翻译
            List<DistributorAccountInfoVo> translationDistributorAccounts = translation(distributorAccounts, merchantIndustryMap, merchantRechargeTotalMap);
            PageInfo pageInfo = new PageInfo(translationDistributorAccounts);
            pageInfo.setTotal(total);
            return ResultMessage.success(pageInfo);
        }
        List<DistributorAccountInfoVo> list = distributorMapper.selectDistributorAccount();
        long total = new PageInfo(list).getTotal();
        //字典翻译
        List<DistributorAccountInfoVo> translationDistributorAccounts = translation(list, merchantIndustryMap, merchantRechargeTotalMap);
        PageInfo pageInfo = new PageInfo(translationDistributorAccounts);
        pageInfo.setTotal(total);
        return ResultMessage.success(pageInfo);
    }

    /**
     * @param list
     * @param merchantIndustryMap
     * @param merchantRechargeTotalMap
     * @ClassName: DistributorAccountServiceImpl
     * @Return: void
     * @Decription: 显示分销商列表的时候加上商户性质和已充值总的金额
     * @CreateDate: Created in 2018/3/20 0020 上午 11:58
     * @Author: <a href="410508961@qq.com">夏凯</a>
     * @Modify:
     */
    public List<DistributorAccountInfoVo> translation(List<DistributorAccountInfoVo> list, Map<String, String> merchantIndustryMap, Map<String, String> merchantRechargeTotalMap) {
        List<DistributorAccountInfoVo> collectDistributorAccountIn = list.stream().map(distributorAccountInfoVo -> {
            String s = merchantIndustryMap.get(distributorAccountInfoVo.getDistributorIndustry());
            if (!(null == s || "".equals(s))) {
                distributorAccountInfoVo.setDistributorIndustry(s);
            }
            String s1 = merchantRechargeTotalMap.get(distributorAccountInfoVo.getDistributorId().toString());
            if (!(null == s1 || "".equals(s1))) {
                distributorAccountInfoVo.setAccountBalanceAlready(new BigDecimal(s1));
            }
            return distributorAccountInfoVo;
        }).collect(Collectors.toList());
        return collectDistributorAccountIn;
    }

    /**
     * @param distributorAccount
     * @ClassName: DistributorAccountServiceImpl
     * @Return: int
     * @Decription: 修改分销商最低余额
     * @CreateDate: Created in 2018/1/17 0017 上午 10:21
     * @Author: <a href="410508961@qq.com">夏凯</a>
     * @Modify:
     */
    public int updateMinBalance(DistributorAccount distributorAccount) {
        return distributorMapper.updateMinBalance(distributorAccount.getId(), distributorAccount.getAccountMinBalance());
    }


    /**
     * @param
     * @ClassName: DistributorAccountServiceImpl
     * @Return: com.cloud.drore.eboss.operation.entity.vo.DistributorAccountVo
     * @Decription: 运营商查询账户信息
     * @CreateDate: Created in 2018/2/9 0009 下午 3:42
     * @Author: <a href="410508961@qq.com">夏凯</a>
     * @Modify:
     */
    public DistributorAccountVo queryAccountDetail() {
        DistributorAccountVo distributorAccountVo = new DistributorAccountVo();
        //查询账户余额信息
        //BigDecimal distriButorAccountTotal = finaceSettlementHistoryMapper.queryFinaceSettlementDistriButorAccountBalance();
        BigDecimal distriButorAccountTotal = finaceSettlementHistoryMapper.queryDistriButorRechargeAlerady();
        //查询供应商已结算信息
        BigDecimal finaceSettlementHistorySupplier = finaceSettlementHistoryMapper.queryFinaceSettlementHistorySettleShould();
        //查询平台实际结算金额信息
        BigDecimal finaceOfficeSettlementHistorySupplier = finaceSettlementHistoryMapper.queryOfflineSettlementHistorySettleShould();
        distributorAccountVo.setTotalAmountAleady(finaceSettlementHistorySupplier);
        if (null == finaceOfficeSettlementHistorySupplier || "".equals(finaceOfficeSettlementHistorySupplier)
                || finaceOfficeSettlementHistorySupplier.toString().equals("0")) {
            distributorAccountVo.setAccountBalance(distriButorAccountTotal);
        } else {
            distributorAccountVo.setAccountBalance(distriButorAccountTotal.subtract(finaceSettlementHistorySupplier));
        }
        distributorAccountVo.setTotalAmountOffice(finaceOfficeSettlementHistorySupplier);
        return distributorAccountVo;
    }

    /**
     * @param id
     * @ClassName: DistributorAccountServiceImpl
     * @Return: com.cloud.drore.eboss.operation.entity.DistributorAccount
     * @Decription: 根据列表id查询分销商账户余额信息
     * @CreateDate: Created in 2018/3/13 0013 下午 4:03
     * @Author: <a href="410508961@qq.com">夏凯</a>
     * @Modify:
     */
    public DistributorAccount queryDistributorAccountAmount(Long id) {
        DistributorAccount distributorAccount = (DistributorAccount) selectByPrimaryKey(id);
        return distributorAccount;
    }

    /**
     * @param id
     * @ClassName: DistributorAccountServiceImpl
     * @Return: com.cloud.drore.eboss.operation.entity.DistributorAccount
     * @Decription: 根据分销商id查询分销商账户余额信息
     * @CreateDate: Created in 2018/3/13 0013 下午 4:03
     * @Author: <a href="410508961@qq.com">夏凯</a>
     * @Modify:
     */
    public DistributorAccount queryDistributorById(Long id) {
        DistributorAccount distributorAccount = distributorMapper.queryDistributorById(id);
        return distributorAccount;
    }
}
